import type { ModelStuck, ModelItem } from '@/types'
import { trim, words } from 'lodash-es'
/**
 * useParseStuck
 * @returns {parseModelStuck}
 */
export const useParseStuckHooks = () => {
  //解析modelstuck
  const parseModelStuck = (modelStuckString: string): ModelStuck => {
    const trimString = trim(modelStuckString)
    const modelName = trimString.split(' ')[0]
    // 获取{中间的数据}
    const content = trim(trimString.split('{')[1].split('}')[0])
    const itemString = content.split('\n')
    const stcukContent = itemString.map((item) => {
      return getStuckFromStringItem(item)
    })
    return {
      modelName,
      content: stcukContent
    }
  }

  return {
    parseModelStuck
  }
}

// 解析单列为ModelItem
const getStuckFromStringItem = (item: string): ModelItem => {
  const trimString = trim(item)
  // 获取备注
  const remark = trim(trimString.split('//')[1])
  const orginWords = words(trimString.split('//')[0])
  const length = orginWords.length
  const splitDbIndex = orginWords.indexOf('db')
  let keyName = ''
  orginWords.forEach((item, index) => {
    if (index > splitDbIndex && index <= length - 1) {
      keyName += item
    }
  })
  // 获取type
  const typeOrigin = orginWords[splitDbIndex - 1]
  console.log('orginWords:', orginWords)

  console.log('typeOrigin:', typeOrigin)

  const type = typeOrigin == '64' ? 'int64' : 'string'
  return {
    name: keyName,
    type: type,
    remark: remark
  }
}
